//
//  LabTextFieldCell.m
//  KuWoCan
//
//  Created by hwd on 17/3/16.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "LabTextFieldCell.h"

@interface LabTextFieldCell ()

@property (nonatomic, copy) NSArray *numberKeyBoardArr;
@property (nonatomic, copy) NSArray *decimalsKeyBoardArr;
@property (nonatomic, strong)UIView *lineView;
@property (nonatomic, strong)UILabel *placeLab;
@end

@implementation LabTextFieldCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI
{
    _leftLab = [UILabel new];
    _leftLab.font = KwFont(14);
    _leftLab.textColor = [UIColor grayColor];
    _leftLab.text = _constStr;
    [self.contentView addSubview:_leftLab];
    [_leftLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@20);
        make.top.equalTo(@20);
        make.height.equalTo(@14);
    }];
    
    _unitLab = [UILabel new];
    _unitLab.font = KwFont(14);
    _unitLab.textColor = KwBlueColor;
    _unitLab.textAlignment = NSTextAlignmentRight;
    _unitLab.text = _unitStr;
    [self.contentView  addSubview:_unitLab];
    [_unitLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-20);
        make.top.equalTo(_leftLab.mas_top);
        make.width.equalTo(@50);
        make.height.equalTo(_leftLab.mas_height);
    }];
    _rightTxtF = [UITextView new];
    _rightTxtF.returnKeyType = UIReturnKeyDone;
    _rightTxtF.delegate = self;
    _rightTxtF.font = KwFont(14);
    _rightTxtF.textColor = [UIColor colorWithHexString:@"55aee9"];
    [self.contentView  addSubview:_rightTxtF];
    
    [_rightTxtF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@115);
        make.width.equalTo(@(SCREENW-195));
        make.top.equalTo(self.contentView).offset(10);
    }];
    
    _placeLab = [[UILabel  alloc]initWithFrame:CGRectMake(5, 8,SCREENW-205,18)];;
    _placeLab.font = KwFont(14);
    _placeLab.textColor = [UIColor lightGrayColor];
    _placeLab.hidden = YES;
    [_rightTxtF  addSubview:_placeLab];
    
    
    _lineView = [UIView new];
    _lineView.backgroundColor = KwGaryColor;
    [self.contentView  addSubview:_lineView];
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@1.5);
        make.left.equalTo(_leftLab.mas_left);
        make.right.equalTo(@-20);
        make.top.equalTo(_rightTxtF.mas_bottom).offset(10);
        make.bottom.equalTo(@0);
    }];
}

- (void)setConstStr:(NSString *)constStr
{
    _leftLab.text = constStr;
    if ([self.decimalsKeyBoardArr containsObject:constStr]) {
        _rightTxtF.keyboardType = UIKeyboardTypeDecimalPad;
    } else if ([self.numberKeyBoardArr containsObject:constStr]) {
        _rightTxtF.keyboardType = UIKeyboardTypeNumberPad;
    } else {
        _rightTxtF.keyboardType = UIKeyboardTypeDefault;
    }
    
    if (comparisonString(constStr, @"费效比率") || comparisonString(constStr, @"期间费效比率")) {
        _unitLab.text = @"%";
    } else if (comparisonString(constStr, @"销售额") || comparisonString(constStr, @"费用预算") || comparisonString(constStr, @"费用总计") || comparisonString(constStr, @"费用") || comparisonString(constStr, @"期间销售额")){
        _unitLab.text = @"元";
    } else if (comparisonString(constStr, @"注册资金")) {
        _unitLab.text = @"万元";
    } else if (comparisonString(constStr, @"数量") || comparisonString(constStr, @"门店数量")) {
        _unitLab.text = @"家";
    } else if (comparisonString(constStr, @"面积")) {
        _unitLab.text = @"平方米";
    } else if (comparisonString(constStr, @"年经营额") || comparisonString(constStr, @"年销售额")) {
        _unitLab.text = @"万元";
    }
    
    else {
        _unitLab.text = @"";
    }
    _placeLab.hidden = NO;
    if (comparisonString(constStr, @"执行时间")) {
        _placeLab.text = @"(例如：4.1-4.30)";;
        _rightTxtF.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    } else if (comparisonString(constStr, @"渠道概况")) {
        _placeLab.text = @"(费用申请同意后方可执行)";;
    } else {
        
        _placeLab.hidden = YES;
        _placeLab.text = @"";
    }
    _rightTxtF.tag = 0;
    
}

- (void)setParaStr:(NSString *)paraStr
{
    _rightTxtF.text = [ToolViewViewController isNotNullStr:paraStr]?paraStr:@" ";
    _placeLab.hidden =  [ToolViewViewController isNotNullStr:paraStr]?YES:NO;
    _rightTxtF.frame = CGRectMake(115,5,SCREENW-195,0) ;
    [_rightTxtF sizeToFit];
    [_rightTxtF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(_rightTxtF.frame.size.height));
    }];
}
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    _placeLab.hidden = YES;
    return YES;
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
    if ([textView.text isEqual:@""] || textView.text.length<=0) {
        _placeLab.hidden = NO;
    }else
    {
        _placeLab.hidden = YES;
    }
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqual:@"\n"]) {
        [_rightTxtF resignFirstResponder];
    }
    return YES;
}
-(void)textViewDidChange:(UITextView *)textView
{
    if (_block)
    {
        if ([textView.text isEqual:@""] || textView.text.length<=0) {
            _placeLab.hidden = NO;
            self.block(@"");
        }else
        {
            _placeLab.hidden = YES;
            self.block(textView.text);
        }
    }
}
- (NSArray *)numberKeyBoardArr {
    if (!_numberKeyBoardArr) {
        _numberKeyBoardArr = @[
                               @"电话",
                               @"手机号码",
                               @"门店数量",
                               @"数量",
                               @"运输车辆",
                               @"团队人数",
                               @"行业经验",
                               @"银行卡号",
                               @"面积"
                               ];
    }
    return _numberKeyBoardArr;
}

- (NSArray *)decimalsKeyBoardArr {
    if (!_decimalsKeyBoardArr) {
        _decimalsKeyBoardArr = @[
                                 @"注册资金",
                                 @"销售额",
                                 @"费效比率",
                                 @"期间费效比率",
                                 @"期间销售额",
                                 @"年经营额",
                                 @"年销售额",
                                 @"费用总计",
                                 @"费用预算",
                                 @"费用",
                                 @"仓库面积"
                                 ];
    }
    return _decimalsKeyBoardArr;
}

@end
